package cn.ctodb.mall.goods.service.impl;

import cn.ctodb.mall.goods.service.GoodsTypeService;
import cn.ctodb.mall.goods.domain.GoodsType;
import cn.ctodb.mall.goods.repository.GoodsTypeRepository;
import cn.ctodb.mall.goods.repository.search.GoodsTypeSearchRepository;
import cn.ctodb.mall.goods.service.dto.GoodsTypeDTO;
import cn.ctodb.mall.goods.service.mapper.GoodsTypeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing GoodsType.
 */
@Service
@Transactional
public class GoodsTypeServiceImpl implements GoodsTypeService{

    private final Logger log = LoggerFactory.getLogger(GoodsTypeServiceImpl.class);
    
    private final GoodsTypeRepository goodsTypeRepository;

    private final GoodsTypeMapper goodsTypeMapper;

    private final GoodsTypeSearchRepository goodsTypeSearchRepository;

    public GoodsTypeServiceImpl(GoodsTypeRepository goodsTypeRepository, GoodsTypeMapper goodsTypeMapper, GoodsTypeSearchRepository goodsTypeSearchRepository) {
        this.goodsTypeRepository = goodsTypeRepository;
        this.goodsTypeMapper = goodsTypeMapper;
        this.goodsTypeSearchRepository = goodsTypeSearchRepository;
    }

    /**
     * Save a goodsType.
     *
     * @param goodsTypeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public GoodsTypeDTO save(GoodsTypeDTO goodsTypeDTO) {
        log.debug("Request to save GoodsType : {}", goodsTypeDTO);
        GoodsType goodsType = goodsTypeMapper.goodsTypeDTOToGoodsType(goodsTypeDTO);
        goodsType = goodsTypeRepository.save(goodsType);
        GoodsTypeDTO result = goodsTypeMapper.goodsTypeToGoodsTypeDTO(goodsType);
        goodsTypeSearchRepository.save(goodsType);
        return result;
    }

    /**
     *  Get all the goodsTypes.
     *  
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<GoodsTypeDTO> findAll(Pageable pageable) {
        log.debug("Request to get all GoodsTypes");
        Page<GoodsType> result = goodsTypeRepository.findAll(pageable);
        return result.map(goodsType -> goodsTypeMapper.goodsTypeToGoodsTypeDTO(goodsType));
    }

    /**
     *  Get one goodsType by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public GoodsTypeDTO findOne(Long id) {
        log.debug("Request to get GoodsType : {}", id);
        GoodsType goodsType = goodsTypeRepository.findOne(id);
        GoodsTypeDTO goodsTypeDTO = goodsTypeMapper.goodsTypeToGoodsTypeDTO(goodsType);
        return goodsTypeDTO;
    }

    /**
     *  Delete the  goodsType by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete GoodsType : {}", id);
        goodsTypeRepository.delete(id);
        goodsTypeSearchRepository.delete(id);
    }

    /**
     * Search for the goodsType corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<GoodsTypeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of GoodsTypes for query {}", query);
        Page<GoodsType> result = goodsTypeSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(goodsType -> goodsTypeMapper.goodsTypeToGoodsTypeDTO(goodsType));
    }
}
